﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBZH_Manage_Order.Models;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers.MerchantsSettings
{
    public class BoxManageController : BaseController
    {
        //
        // GET: /BoxManage/

        ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();
        OrgDC.Service1Client orgdc = new OrgDC.Service1Client();

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取管理员下面的商户
        /// </summary>
        /// <returns></returns>
        public ActionResult getmer()
        {
            try
            {
                Page<Model.OrgOrder_Mer_T> page = new Page<Model.OrgOrder_Mer_T>();
                List<Model.OrgOrder_Mer_T> list = new List<Model.OrgOrder_Mer_T>();//定义商品分类设置
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];

                if (user == null)
                {
                    return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面
                }

                string result = service.GetMer(user.OperID, "", 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    var a = searcho["rows"].Children().ToJson();
                    return Json(a, JsonRequestBehavior.AllowGet);
                }
                return Json(searcho["Msg"].ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 根据商户id获取包厢信息
        /// </summary>
        /// <param name="merid"></param>
        /// <returns></returns>
        public ActionResult getbox(string merid, string name, JqGridParam jqgridparam)
        {
            try
            {
                List<Model.OrgOrder_Box_T> list = new List<Model.OrgOrder_Box_T>();
                string result = orgdc.GetBox(jqgridparam.page, jqgridparam.rows, merid, name, YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();//获取返回结果是否正常标识
                if (bs == "1")//返回结果正常
                {
                    IList<JToken> jieguo = searcho["rows"].Children().ToList();//解析返回的结果
                    list = ParseClass<Model.OrgOrder_Box_T>.JsonList(jieguo);//解析返回的结果\

                    List<newbox> listnewbox = new List<newbox>();

                    foreach (var item in list)
                    {
                        newbox nb = new newbox();
                        nb.BoxID = item.BoxID;
                        nb.BoxName = item.BoxName;
                        if (item.BoxStat == 0)
                        {
                            nb.stat = "未使用";
                        }
                        else
                        {
                            nb.stat = "已使用";
                        }
                        nb.BZ = item.BZ;
                        nb.BoxStat = item.BoxStat;
                        nb.MerID = item.MerID;
                        nb.State = item.State;
                        listnewbox.Add(nb);
                    }
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = listnewbox
                    };
                    return Json(data);
                }
                else
                {
                    var data = new
                    {
                        total = searcho["total"].ToString(),
                        rows = list
                    };
                    return Json(data);
                }
            }
            catch (Exception)
            {

                throw;
            }
            return Content("");
        }

        [HttpGet]
        public string ReturnIsShowData()
        {
            string strJson = "[{\"BoxStat\":\"0\",\"text\":\"未使用\"},{\"BoxStat\":\"1\",\"text\":\"已使用\"}]";
            return strJson;  
        }

        /// <summary>
        /// 更新包厢状态action
        /// </summary>
        /// <param name="tag"></param>
        /// <param name="box"></param>
        /// <returns></returns>
        public ActionResult updateBox(int tag, Model.OrgOrder_Box_T box)
        {
            try
            {
                if (tag == 3)//删除
                {
                    box.State = 0;
                }
                Model.View_OperUser user = (Model.View_OperUser)Session[SessionStorage.OperUser];
                string result = orgdc.UpdateBox(user.OperID, box.ToJson(), tag, YZM, 1);
                JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                string bs = searcho["BS"].ToString();
                if (bs == "1")
                {
                    return Content(bs);
                }
                else
                {
                    return Content(searcho["Msg"].ToString());
                }
            }
            catch (Exception)
            {
                return RedirectToAction("LoginPage", "Login");  //czx:修改为登录者的Session异常时，直接跳转到登录页面

            }
        }

        public class newbox
        {
            public int BoxID { get; set; }
            public string BoxName { get; set; }
            public int BoxStat { get; set; }
            public string BZ { get; set; }
            public int MerID { get; set; }
            public string stat { get; set; }
            public int State { get; set; }
        }

    }
}
